<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>大数据之企业级搜索引擎 ElashticSearch | Oct25-X</title><meta name="keywords" content="Elasticsearch"><meta name="author" content="Oct25-X"><meta name="copyright" content="Oct25-X"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Elasticsearch 简介ElasticSearch 是一个基于 Lucene 的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎，基于 RESTful web 接口。Elasticsearch 是用 Java 开发的，并作为 Apache 许可条款下的开放源码发布，是当前流行的企业级搜索引擎。  空间换时间，将数据放到  ES中，数据会扩大 5-10 倍，原因在于 ES 会建立很多索">
<meta property="og:type" content="article">
<meta property="og:title" content="大数据之企业级搜索引擎 ElashticSearch">
<meta property="og:url" content="https://oct25-x.atomgit.net/oct25-xxxxx/2023/10/24/33%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E4%BC%81%E4%B8%9A%E7%BA%A7%E6%90%9C%E7%B4%A2%E5%BC%95%E6%93%8E%20ElashticSearch_7.9.3/index.html">
<meta property="og:site_name" content="Oct25-X">
<meta property="og:description" content="Elasticsearch 简介ElasticSearch 是一个基于 Lucene 的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎，基于 RESTful web 接口。Elasticsearch 是用 Java 开发的，并作为 Apache 许可条款下的开放源码发布，是当前流行的企业级搜索引擎。  空间换时间，将数据放到  ES中，数据会扩大 5-10 倍，原因在于 ES 会建立很多索">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://oct25-x.atomgit.net/oct25-xxxxx/img/cover_img/ctt33.png">
<meta property="article:published_time" content="2023-10-23T23:12:33.000Z">
<meta property="article:modified_time" content="2024-02-17T14:28:40.780Z">
<meta property="article:author" content="Oct25-X">
<meta property="article:tag" content="Elasticsearch">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://oct25-x.atomgit.net/oct25-xxxxx/img/cover_img/ctt33.png"><link rel="shortcut icon" href="/oct25-xxxxx/img/logo.jpg"><link rel="canonical" href="https://oct25-x.atomgit.net/oct25-xxxxx/2023/10/24/33%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E4%BC%81%E4%B8%9A%E7%BA%A7%E6%90%9C%E7%B4%A2%E5%BC%95%E6%93%8E%20ElashticSearch_7.9.3/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/oct25-xxxxx/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/oct25-xxxxx/',
  algolia: undefined,
  localSearch: {"path":"search.json","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '大数据之企业级搜索引擎 ElashticSearch',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-02-17 22:28:40'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/rightMenu.css"><meta name="generator" content="Hexo 6.1.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/oct25-xxxxx/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/oct25-xxxxx/archives/"><div class="headline">文章</div><div class="length-num">52</div></a></div><div class="data-item"><a href="/oct25-xxxxx/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div><div class="data-item"><a href="/oct25-xxxxx/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/oct25-xxxxx/img/top_img/top_bg7.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/oct25-xxxxx/">Oct25-X</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">大数据之企业级搜索引擎 ElashticSearch</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-10-23T23:12:33.000Z" title="发表于 2023-10-24 07:12:33">2023-10-24</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-02-17T14:28:40.780Z" title="更新于 2024-02-17 22:28:40">2024-02-17</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/oct25-xxxxx/categories/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E7%BB%84%E4%BB%B6/">微服务组件</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="大数据之企业级搜索引擎 ElashticSearch"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Elasticsearch-简介"><a href="#Elasticsearch-简介" class="headerlink" title="Elasticsearch 简介"></a>Elasticsearch 简介</h1><p>ElasticSearch 是一个基于 Lucene 的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎，基于 RESTful web 接口。Elasticsearch 是用 Java 开发的，并作为 Apache 许可条款下的开放源码发布，是当前流行的企业级搜索引擎。 </p>
<p><strong>空间换时间，将数据放到  ES中，数据会扩大 5-10 倍，原因在于 ES 会建立很多索引。</strong></p>
<h2 id="一、ElashticSearch-使用场景"><a href="#一、ElashticSearch-使用场景" class="headerlink" title="一、ElashticSearch 使用场景"></a>一、ElashticSearch 使用场景</h2><ol>
<li>为用户提供按关键字查询的全文搜索功能。</li>
<li>实现企业海量数据的处理分析的解决方案。大数据领域的重要一份子，如著名的 <strong>ELK （ElasticSearch,Logstash,Kibana）框架</strong>。</li>
</ol>
<h2 id="二、与其他数据存储进行比较"><a href="#二、与其他数据存储进行比较" class="headerlink" title="二、与其他数据存储进行比较"></a>二、与其他数据存储进行比较</h2><table>
<thead>
<tr>
<th></th>
<th>Redis</th>
<th>MySQL</th>
<th>ElashticSearch</th>
<th>HBase</th>
<th>Hadoop&#x2F;Hive</th>
</tr>
</thead>
<tbody><tr>
<td>容量&#x2F;容量扩展</td>
<td>低</td>
<td>中</td>
<td>较大</td>
<td>海量</td>
<td>海量</td>
</tr>
<tr>
<td>查询时效性</td>
<td>极高</td>
<td>中等</td>
<td>较高</td>
<td>中等</td>
<td>低</td>
</tr>
<tr>
<td>查询灵活性</td>
<td>较差，K-V模式</td>
<td>非常好，支持SQL</td>
<td>较好，关联查询（join）较弱，但是可以全文检索，DSL语言可以处理过滤、匹配、排序、聚合等操作</td>
<td>较差，主要靠Rowkey，Scan全表性能低，或者使用Phoenix建立二级索引</td>
<td>非常好，支持SQL</td>
</tr>
<tr>
<td>写入速度</td>
<td>极快</td>
<td>中等</td>
<td>较快</td>
<td>较快</td>
<td>慢</td>
</tr>
<tr>
<td>一致性、事务</td>
<td>弱</td>
<td>强</td>
<td>弱</td>
<td>弱</td>
<td>弱</td>
</tr>
</tbody></table>
<p><strong>为什么 ES 的查询与写入都比 MySQL 快？</strong></p>
<ol>
<li>ES 写入采用的是异步写入方式（写入响应快，但是落盘有延迟），提高了写入速度；</li>
<li>ES 查询采用了倒排索引；</li>
</ol>
<p><strong>注：ES 和 HBase 都是异步写入，存在写入延迟；</strong></p>
<h2 id="三、ElashticSearch-的特点"><a href="#三、ElashticSearch-的特点" class="headerlink" title="三、ElashticSearch 的特点"></a>三、ElashticSearch 的特点</h2><h3 id="天然分片，天然集群"><a href="#天然分片，天然集群" class="headerlink" title="天然分片，天然集群"></a>天然分片，天然集群</h3><p>ES 会把数据进行分片，将多个分片存放到不同服务器上，以达到负载均衡，横向扩展；</p>
<p>同时，ES 集群会的分片数据会存在副本，分片数据与其副本也存放在不同服务器上，以达到高可用作用；</p>
<p><img src="/oct25-xxxxx/img/es/es01.png"></p>
<p>P &#x3D;&gt; Primary，R &#x3D;&gt; Replication</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ES6.6 默认5个分片，5个副本</span><br><span class="line">ES7.0 默认1个分片</span><br></pre></td></tr></table></figure>

<p>在实际运算过程中，每个查询任务提交到<strong>某一个节点（MASTER）</strong>，该节点必须负责将数据进行整理汇聚，再返回给客户端，也就是一个简单的节点上进行 Map 计算，在一个固定的节点上进行 Reduces 得到最终结果向客户端返回。</p>
<p><strong>&#x3D;&#x3D;这种集群分片的机制造就了elasticsearch强大的数据容量及运算扩展性。&#x3D;&#x3D;</strong></p>
<h3 id="天然索引"><a href="#天然索引" class="headerlink" title="天然索引"></a>天然索引</h3><p>ES 所有数据都是<strong>默认进行索引</strong>的，这点和 MySQL 正好相反，MySQL  是默认不加索引，要加索引必须特别说明，<strong>ES 只有不加索引才需要说明</strong>。而ES使用的是倒排索引和 MySQL 的 B+Tree 索引不同。</p>
<h4 id="传统关系型数据库"><a href="#传统关系型数据库" class="headerlink" title="传统关系型数据库"></a>传统关系型数据库</h4><ol>
<li>对于传统的关系性数据库对于关键词的查询，<strong>只能逐字逐行的匹配，性能非常差</strong>。</li>
<li><strong>匹配方式不合理</strong>，比如搜索“小米手机” ，如果用like进行匹配， 根本匹配不到。但是考虑使用者的用户体验的话，除了完全匹配的记录，还应该显示一部分近似匹配的记录，至少应该匹配到“手机”。</li>
</ol>
<h4 id="倒排索引是如何处理的"><a href="#倒排索引是如何处理的" class="headerlink" title="倒排索引是如何处理的"></a>倒排索引是如何处理的</h4><ol>
<li><p>全文搜索引擎目前主流的索引技术就是倒排索引的方式；</p>
</li>
<li><p>传统的保存数据的方式都是：记录 &#x3D;&gt; 单词；</p>
</li>
<li><p>而倒排索引的保存数据的方式是：单词 &#x3D;&gt; 记录；</p>
</li>
</ol>
<h4 id="索引结构对比"><a href="#索引结构对比" class="headerlink" title="索引结构对比"></a>索引结构对比</h4><p><strong>B+Tree</strong></p>
<p><img src="/oct25-xxxxx/img/es/es02.png"></p>
<p><strong>Lucene 倒排索引</strong></p>
<p>ES &#x2F; Solr 都是基于 Lucene 做的，Lucene 是一个提供全文搜索功能类库的核心工具包；</p>
<p><img src="/oct25-xxxxx/img/es/es03.png"></p>
<p>**&#x3D;&#x3D;Term Index 用于快速定位索引 Term Dictionary 的，而 Term Index 是缓存在内存中的&#x3D;&#x3D;**，但是 MySQL 的 B+Tree 不在内存中，所以整体来看ES速度更快，但同时也更消耗资源（内存、磁盘）。</p>
<h1 id="Elasticsearch-安装"><a href="#Elasticsearch-安装" class="headerlink" title="Elasticsearch 安装"></a>Elasticsearch 安装</h1><h2 id="一、相关下载"><a href="#一、相关下载" class="headerlink" title="一、相关下载"></a>一、相关下载</h2><p>Elasticsearch 下载地址：<a target="_blank" rel="noopener" href="https://www.elastic.co/cn/downloads/elasticsearch">https://www.elastic.co/cn/downloads/elasticsearch</a></p>
<p>ES 分词器 IK 下载地址：<a target="_blank" rel="noopener" href="https://github.com/medcl/elasticsearch-analysis-ik/releases">https://github.com/medcl/elasticsearch-analysis-ik/releases</a></p>
<p>Kibana 下载地址：<a target="_blank" rel="noopener" href="https://www.elastic.co/cn/downloads/kibana">https://www.elastic.co/cn/downloads/kibana</a></p>
<p>本文选择 Elasticsearch 版本为 7.9.3</p>
<h2 id="二、Windows-安装-Elasticsearch"><a href="#二、Windows-安装-Elasticsearch" class="headerlink" title="二、Windows 安装 Elasticsearch"></a>二、Windows 安装 Elasticsearch</h2><ol>
<li>Elasticsearch 下载解压后，直接进入 bin&#x2F; 目录下，双击 <code>elasticsearch.bat</code> 即可启动</li>
</ol>
<p>浏览器访问：<a target="_blank" rel="noopener" href="http://localhost:9200/">http://localhost:9200</a></p>
<ol start="2">
<li>Elasticsearch 中文分词器，将下载好的 zip 压缩包直接解压到 elasticsearch&#x2F;plugins&#x2F;ik&#x2F; 目录下即可</li>
<li>如果使用 kibana 作为客户端操作 ES 的话，跟 ES 一样，下载解压后，直接进入到 bin&#x2F; 目录下，双击 <code>kibana.bat</code> 启动即可</li>
</ol>
<p>浏览器访问：<a target="_blank" rel="noopener" href="http://localhost:5601/">http://localhost:5601</a> ，即可进入 kibana WebUI 界面</p>
<h2 id="三、Linux-安装-Elasticsearch"><a href="#三、Linux-安装-Elasticsearch" class="headerlink" title="三、Linux 安装 Elasticsearch"></a>三、Linux 安装 Elasticsearch</h2><h3 id="修改-Linux-配置"><a href="#修改-Linux-配置" class="headerlink" title="修改 Linux 配置"></a>修改 Linux 配置</h3><p>为什么要修改linux配置？</p>
<p>默认elasticsearch是单机访问模式，就是只能自己访问自己。</p>
<p>但是我们之后一定会设置成允许应用服务器通过网络方式访问。</p>
<p>这时，ElasticSearch就会因为嫌弃单机版的低端默认配置而报错，甚至无法启动。</p>
<p>所以我们在这里就要<strong>把服务器的一些限制打开，能支持更多并发</strong>。</p>
<h4 id="问题1"><a href="#问题1" class="headerlink" title="问题1"></a>问题1</h4><p><strong>max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536] elasticsearch</strong></p>
<p>原因：系统允许 Elasticsearch 打开的最大文件数需要修改成 65536</p>
<p>解决：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo vim /etc/security/limits.conf</span><br></pre></td></tr></table></figure>

<p>添加内容：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">* soft nofile 65536</span><br><span class="line">* hard nofile 131072</span><br><span class="line">* soft <span class="built_in">nproc</span> 2048</span><br><span class="line">* hard <span class="built_in">nproc</span> 65536</span><br></pre></td></tr></table></figure>

<p>注意：* 号不要省略</p>
<h4 id="问题2"><a href="#问题2" class="headerlink" title="问题2"></a>问题2</h4><p><strong>max number of threads [1024] for user [judy2] likely too low, increase to at least [4096]</strong></p>
<p>原因：允许最大进程数修该成 4096</p>
<p>解决：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo vim /etc/security/limits.d/90-nproc.conf</span><br></pre></td></tr></table></figure>


<p>修改如下内容：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">* soft <span class="built_in">nproc</span> 1024</span><br><span class="line"><span class="comment">#修改为</span></span><br><span class="line">* soft <span class="built_in">nproc</span> 4096</span><br></pre></td></tr></table></figure>

<p>注：CentOS7 不需要修改</p>
<h4 id="问题3"><a href="#问题3" class="headerlink" title="问题3"></a>问题3</h4><p> <strong>max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]</strong> </p>
<p>原因：一个进程可以拥有的虚拟内存区域的数量。</p>
<p>解决：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo vim /etc/sysctl.conf</span><br></pre></td></tr></table></figure>


<p>添加内容：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vm.max_map_count=262144</span><br></pre></td></tr></table></figure>

<p>即可永久修改</p>
<p>注：CentOS7 不需要修改</p>
<h3 id="Elasticsearch-安装-1"><a href="#Elasticsearch-安装-1" class="headerlink" title="Elasticsearch 安装"></a>Elasticsearch 安装</h3><ol>
<li>上传解压</li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tar -zxvf elasticsearch-7.9.3-linux-x86_64.tar.gz -C /opt/module</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>修改配置文件</li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim ./config/elashticsearch.yml</span><br></pre></td></tr></table></figure>

<p>修改注意事项：</p>
<ul>
<li><p>每行必须顶格，不能有空格；</p>
</li>
<li><p>: 号后面必须有一个空格；</p>
</li>
</ul>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 集群名称，同一集群名称必须相同</span></span><br><span class="line"><span class="comment"># Use a descriptive name for your cluster:</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="attr">cluster.name:</span> <span class="string">my-es</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 单个节点名称</span></span><br><span class="line"><span class="comment"># Use a descriptive name for the node:</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="attr">node.name:</span> <span class="string">node-1</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 网络部分，改为当前ip地址，端口号保持默认 9200 即可</span></span><br><span class="line"><span class="comment"># Set the bind address to a specific IP (IPv4 or IPv6):</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="attr">network.host:</span> <span class="string">hadoop102</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 把 bootstrap 自检程序关掉</span></span><br><span class="line"><span class="comment"># Lock the memory on startup:</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="attr">bootstrap.memory_lock:</span> <span class="literal">false</span></span><br><span class="line"><span class="attr">bootstrap.system_call_filter:</span> <span class="literal">false</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 自发现配置：新节点向集群报到的主机名</span></span><br><span class="line"><span class="comment"># Pass an initial list of hosts to perform discovery when new node is started:</span></span><br><span class="line"><span class="comment"># The default list of host is [&quot;127.0.0.1&quot;, &quot;[::1]&quot;]</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="attr">discovery.zen.ping.unicast.hosts:</span> [<span class="string">&quot;hadoop102&quot;</span>, <span class="string">&quot;hadoop103&quot;</span>, <span class="string">&quot;hadoop104&quot;</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 最小节点数：防止脑裂，设置为半数以上</span></span><br><span class="line"><span class="comment"># Prevent the &quot;split brain&quot; by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):</span></span><br><span class="line"><span class="attr">discovery.zen.minimum_master_nodes:</span> <span class="number">2</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 集群名，节点之间要保持一致</span></span><br><span class="line"><span class="comment"># Use a descriptive name for your cluster:</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="attr">cluster.name:</span> <span class="string">my-application</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 节点名，集群内唯一</span></span><br><span class="line"><span class="comment"># Use a descriptive name for the node:</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="attr">node.name:</span> <span class="string">node-1</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 允许访问的ip,0.0.0.0代表允许任意ip访问</span></span><br><span class="line"><span class="comment"># Set the bind address to a specific IP (IPv4 or IPv6):</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="attr">network.host:</span> <span class="number">0.0</span><span class="number">.0</span><span class="number">.0</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 指定该节点是否有资格被选举成为master，默认是true，es是默认集群中的第一台机器为master，如果这台机挂了就会重新选举master</span></span><br><span class="line"><span class="comment"># Add custom attributes to the node:</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="attr">node.master:</span> <span class="literal">true</span></span><br><span class="line"><span class="comment"># 指定该节点是否存储索引数据，默认为true</span></span><br><span class="line"><span class="attr">node.data:</span> <span class="literal">true</span></span><br><span class="line"><span class="attr">node.max_local_storage_nodes:</span> <span class="number">3</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 写入候选主节点的设备地址，在开启服务后可以被选为主节点</span></span><br><span class="line"><span class="comment"># Pass an initial list of hosts to perform discovery when this node is started:</span></span><br><span class="line"><span class="comment"># The default list of hosts is [&quot;127.0.0.1&quot;, &quot;[::1]&quot;]</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="attr">discovery.seed_hosts:</span> [<span class="string">&quot;hadoop102&quot;</span>, <span class="string">&quot;hadoop103&quot;</span>, <span class="string">&quot;hadoop104&quot;</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 初始化一个新的集群时需要此配置来选举master</span></span><br><span class="line"><span class="comment"># Bootstrap the cluster using an initial set of master-eligible nodes:</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="attr">cluster.initial_master_nodes:</span> [<span class="string">&quot;node-1&quot;</span>, <span class="string">&quot;node-2&quot;</span>, <span class="string">&quot;node-3&quot;</span>]</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>虚拟机环境修改</li>
</ol>
<p>ES 是用在 Java 虚拟机中运行的，虚拟机默认启动占用2G内存。但是如果是装在PC机学习用，实际用不了2个G。所以可以改小一点内存。</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim /opt/module/elasticsearch-7.9.3/config/jvm.options</span><br></pre></td></tr></table></figure>

<p>修改以下配置：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Xms represents the initial size of total</span></span><br><span class="line"><span class="comment"># Xmx represents the maximum size of total</span></span><br><span class="line">-Xms256m</span><br><span class="line">-Xmx256m</span><br></pre></td></tr></table></figure>

<ol start="4">
<li>重启 Linux</li>
</ol>
<h3 id="启动与关闭"><a href="#启动与关闭" class="headerlink" title="启动与关闭"></a>启动与关闭</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./bin/elasticsearch</span><br></pre></td></tr></table></figure>

<h3 id="Elasticsearch-集群"><a href="#Elasticsearch-集群" class="headerlink" title="Elasticsearch 集群"></a>Elasticsearch 集群</h3><p>按照上面配置修改完成后，将 ES 分发，然后修改以下配置即可</p>
<ul>
<li>单节点名称：node.name: node-X</li>
<li>网络主机名称：network.host: hadoop10X</li>
</ul>
<p><strong>集群启停脚本</strong></p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash </span></span><br><span class="line">es_home=/opt/module/elasticsearch-7.9.3</span><br><span class="line"></span><br><span class="line"><span class="keyword">case</span> <span class="variable">$1</span>  <span class="keyword">in</span></span><br><span class="line"> <span class="string">&quot;start&quot;</span>) &#123;</span><br><span class="line">  <span class="keyword">for</span> i <span class="keyword">in</span> hadoop102 hadoop103 hadoop104</span><br><span class="line">  <span class="keyword">do</span></span><br><span class="line">    ssh <span class="variable">$i</span>  <span class="string">&quot;source /etc/profile;<span class="variable">$&#123;es_home&#125;</span>/bin/elasticsearch &gt;/dev/null 2&gt;&amp;1 &amp;&quot;</span></span><br><span class="line">  <span class="keyword">done</span></span><br><span class="line">&#125;;;</span><br><span class="line"><span class="string">&quot;stop&quot;</span>) &#123;</span><br><span class="line">  <span class="keyword">for</span> i <span class="keyword">in</span> hadoop102 hadoop103 hadoop104</span><br><span class="line">  <span class="keyword">do</span></span><br><span class="line">      ssh <span class="variable">$i</span> <span class="string">&quot;ps -ef | grep <span class="variable">$es_home</span> | grep -v grep | awk &#x27;&#123;print \$2&#125;&#x27; | xargs kill&quot;</span> &gt;/dev/null 2&gt;&amp;1</span><br><span class="line">  <span class="keyword">done</span></span><br><span class="line">  </span><br><span class="line">&#125;;;</span><br><span class="line"><span class="keyword">esac</span></span><br></pre></td></tr></table></figure>

<h3 id="测试-ES"><a href="#测试-ES" class="headerlink" title="测试 ES"></a>测试 ES</h3><figure class="highlight http"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">curl http://hadoop102:9200/_cat/nodes?v</span><br></pre></td></tr></table></figure>

<p>输出各节点信息即配置成功</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">ip            heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name</span><br><span class="line">192.168.8.104           41          74  16    0.36    0.21     0.16 dilmrt    -      node-3</span><br><span class="line">192.168.8.103           35          72  22    0.35    0.19     0.15 dilmrt    *      node-2</span><br><span class="line">192.168.8.102           25          70  20    0.60    0.22     0.17 dilmrt    -      node-1</span><br></pre></td></tr></table></figure>

<h2 id="四、Kibana-安装"><a href="#四、Kibana-安装" class="headerlink" title="四、Kibana 安装"></a>四、Kibana 安装</h2><ol>
<li>上传解压</li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tar -zxvf kibana-7.9.3-linux-x86_64.tar.gz -C /opt/module/</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>修改配置文件</li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim ./config/kibana.yml</span><br></pre></td></tr></table></figure>

<p>修改以下配置</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">server.host: <span class="string">&quot;0.0.0.0&quot;</span></span><br><span class="line">elasticsearch.hosts: [<span class="string">&quot;http://hadoop102:9200&quot;</span>]</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>启动</li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./bin/kibana</span><br></pre></td></tr></table></figure>

<ol start="4">
<li>启动脚本</li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash </span></span><br><span class="line">es_home=/opt/module/elasticsearch-7.9.3</span><br><span class="line">kibana_home=/opt/module/kibana-7.9.3</span><br><span class="line"></span><br><span class="line"><span class="keyword">case</span> <span class="variable">$1</span>  <span class="keyword">in</span></span><br><span class="line"> <span class="string">&quot;start&quot;</span>) &#123;</span><br><span class="line">  <span class="keyword">for</span> i <span class="keyword">in</span> hadoop102 hadoop103 hadoop104</span><br><span class="line">  <span class="keyword">do</span></span><br><span class="line">    ssh <span class="variable">$i</span>  <span class="string">&quot;source /etc/profile;<span class="variable">$&#123;es_home&#125;</span>/bin/elasticsearch &gt;/dev/null 2&gt;&amp;1 &amp;&quot;</span></span><br><span class="line">  <span class="keyword">done</span></span><br><span class="line">  <span class="built_in">nohup</span> <span class="variable">$&#123;kibana_home&#125;</span>/bin/kibana &gt;<span class="variable">$&#123;kibana_home&#125;</span>/logs/kibana.log 2&gt;&amp;1 &amp;</span><br><span class="line">&#125;;;</span><br><span class="line"><span class="string">&quot;stop&quot;</span>) &#123;</span><br><span class="line">  ps -ef | grep -v grep | awk <span class="string">&#x27;&#123;print $2&#125;&#x27;</span> | xargs <span class="built_in">kill</span></span><br><span class="line">  <span class="keyword">for</span> i <span class="keyword">in</span> hadoop102 hadoop103 hadoop104</span><br><span class="line">  <span class="keyword">do</span></span><br><span class="line">      ssh <span class="variable">$i</span> <span class="string">&quot;ps -ef | grep <span class="variable">$es_home</span> | grep -v grep | awk &#x27;&#123;print \$2&#125;&#x27; | xargs kill&quot;</span> &gt;/dev/null 2&gt;&amp;1</span><br><span class="line">  <span class="keyword">done</span></span><br><span class="line">  </span><br><span class="line">&#125;;;</span><br><span class="line"><span class="keyword">esac</span></span><br></pre></td></tr></table></figure>

<ol start="5">
<li>测试访问：<a target="_blank" rel="noopener" href="http://locahost:5601/">http://locahost:5601</a></li>
</ol>
<h2 id="五、Elasticsearch-中文分词器安装"><a href="#五、Elasticsearch-中文分词器安装" class="headerlink" title="五、Elasticsearch 中文分词器安装"></a>五、Elasticsearch 中文分词器安装</h2><p>解压后，直接将解压文件添加到 ${ES_HOME}&#x2F;plugins&#x2F;ik&#x2F; 中即可</p>
<p>然后重启 ES，测试是否配置成功</p>
<p><strong>最大分词</strong></p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">GET _analyze</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;analyzer&quot;</span>: <span class="string">&quot;ik_max_word&quot;</span>,</span><br><span class="line">  <span class="string">&quot;text&quot;</span>: <span class="string">&quot;我是中国人&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#响应</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;tokens&quot;</span> : [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="string">&quot;token&quot;</span> : <span class="string">&quot;我&quot;</span>,</span><br><span class="line">      <span class="string">&quot;start_offset&quot;</span> : 0,</span><br><span class="line">      <span class="string">&quot;end_offset&quot;</span> : 1,</span><br><span class="line">      <span class="string">&quot;type&quot;</span> : <span class="string">&quot;CN_CHAR&quot;</span>,</span><br><span class="line">      <span class="string">&quot;position&quot;</span> : 0</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="string">&quot;token&quot;</span> : <span class="string">&quot;是&quot;</span>,</span><br><span class="line">      <span class="string">&quot;start_offset&quot;</span> : 1,</span><br><span class="line">      <span class="string">&quot;end_offset&quot;</span> : 2,</span><br><span class="line">      <span class="string">&quot;type&quot;</span> : <span class="string">&quot;CN_CHAR&quot;</span>,</span><br><span class="line">      <span class="string">&quot;position&quot;</span> : 1</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="string">&quot;token&quot;</span> : <span class="string">&quot;中国人&quot;</span>,</span><br><span class="line">      <span class="string">&quot;start_offset&quot;</span> : 2,</span><br><span class="line">      <span class="string">&quot;end_offset&quot;</span> : 5,</span><br><span class="line">      <span class="string">&quot;type&quot;</span> : <span class="string">&quot;CN_WORD&quot;</span>,</span><br><span class="line">      <span class="string">&quot;position&quot;</span> : 2</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="string">&quot;token&quot;</span> : <span class="string">&quot;中国&quot;</span>,</span><br><span class="line">      <span class="string">&quot;start_offset&quot;</span> : 2,</span><br><span class="line">      <span class="string">&quot;end_offset&quot;</span> : 4,</span><br><span class="line">      <span class="string">&quot;type&quot;</span> : <span class="string">&quot;CN_WORD&quot;</span>,</span><br><span class="line">      <span class="string">&quot;position&quot;</span> : 3</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="string">&quot;token&quot;</span> : <span class="string">&quot;国人&quot;</span>,</span><br><span class="line">      <span class="string">&quot;start_offset&quot;</span> : 3,</span><br><span class="line">      <span class="string">&quot;end_offset&quot;</span> : 5,</span><br><span class="line">      <span class="string">&quot;type&quot;</span> : <span class="string">&quot;CN_WORD&quot;</span>,</span><br><span class="line">      <span class="string">&quot;position&quot;</span> : 4</span><br><span class="line">    &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>最小分词</strong></p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">GET _analyze</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;analyzer&quot;</span>: <span class="string">&quot;ik_smart&quot;</span>,</span><br><span class="line">  <span class="string">&quot;text&quot;</span>: <span class="string">&quot;我是中国人&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#响应</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;tokens&quot;</span> : [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="string">&quot;token&quot;</span> : <span class="string">&quot;我&quot;</span>,</span><br><span class="line">      <span class="string">&quot;start_offset&quot;</span> : 0,</span><br><span class="line">      <span class="string">&quot;end_offset&quot;</span> : 1,</span><br><span class="line">      <span class="string">&quot;type&quot;</span> : <span class="string">&quot;CN_CHAR&quot;</span>,</span><br><span class="line">      <span class="string">&quot;position&quot;</span> : 0</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="string">&quot;token&quot;</span> : <span class="string">&quot;是&quot;</span>,</span><br><span class="line">      <span class="string">&quot;start_offset&quot;</span> : 1,</span><br><span class="line">      <span class="string">&quot;end_offset&quot;</span> : 2,</span><br><span class="line">      <span class="string">&quot;type&quot;</span> : <span class="string">&quot;CN_CHAR&quot;</span>,</span><br><span class="line">      <span class="string">&quot;position&quot;</span> : 1</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="string">&quot;token&quot;</span> : <span class="string">&quot;中国人&quot;</span>,</span><br><span class="line">      <span class="string">&quot;start_offset&quot;</span> : 2,</span><br><span class="line">      <span class="string">&quot;end_offset&quot;</span> : 5,</span><br><span class="line">      <span class="string">&quot;type&quot;</span> : <span class="string">&quot;CN_WORD&quot;</span>,</span><br><span class="line">      <span class="string">&quot;position&quot;</span> : 2</span><br><span class="line">    &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h1 id="ElashticSearch-的基本概念"><a href="#ElashticSearch-的基本概念" class="headerlink" title="ElashticSearch 的基本概念"></a>ElashticSearch 的基本概念</h1><table>
<thead>
<tr>
<th>字段</th>
<th>说明</th>
<th>对应MySQL概念</th>
</tr>
</thead>
<tbody><tr>
<td>cluster</td>
<td>ES集群</td>
<td></td>
</tr>
<tr>
<td>node</td>
<td>ES集群中的节点（默认一个进程就是一个节点）</td>
<td></td>
</tr>
<tr>
<td>shard</td>
<td>ES数据分片（默认5片）</td>
<td></td>
</tr>
<tr>
<td>index</td>
<td>类似RDBMS中的数据库（ES5.x，在ES6.x和ES7.x中相当于table）</td>
<td>database</td>
</tr>
<tr>
<td>type</td>
<td>类似RDBMS中的表（ES6.x一个index只允许建立一个type，ES7.x废弃）</td>
<td>table</td>
</tr>
<tr>
<td>document</td>
<td>类似RDBMS中的行</td>
<td>row</td>
</tr>
<tr>
<td>field</td>
<td>相当于字段、属性</td>
<td>column</td>
</tr>
</tbody></table>
<h1 id="Elasticsearch-Restful-Api-DSL"><a href="#Elasticsearch-Restful-Api-DSL" class="headerlink" title="Elasticsearch Restful Api (DSL)"></a>Elasticsearch Restful Api (DSL)</h1><p>DSL 全称 Domain Specific Language，即特定领域专用语言。</p>
<p>ES中保存的数据结构</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Java数据结构</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span>  <span class="title class_">Movie</span> &#123;</span><br><span class="line">	 String id;</span><br><span class="line">     String name;</span><br><span class="line">     Double doubanScore;</span><br><span class="line">     List&lt;Actor&gt; actorList;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Actor</span>&#123;</span><br><span class="line">    String id;</span><br><span class="line">    String name;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这两个 Java 对象如果放在关系型数据库中，会被折成两张表，而在 ES 中，是用一个 Json 来表示 一个 document 的。</p>
<p>所有保存到 ES 中应该是下面这样：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">	<span class="attr">&quot;id&quot;</span><span class="punctuation">:</span><span class="string">&quot;1&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;name&quot;</span><span class="punctuation">:</span><span class="string">&quot;operation red sea&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;doubanScore&quot;</span><span class="punctuation">:</span><span class="string">&quot;8.5&quot;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;actorList&quot;</span><span class="punctuation">:</span><span class="punctuation">[</span></span><br><span class="line">        <span class="punctuation">&#123;</span><span class="attr">&quot;id&quot;</span><span class="punctuation">:</span><span class="string">&quot;1&quot;</span><span class="punctuation">,</span> <span class="attr">&quot;name&quot;</span><span class="punctuation">:</span><span class="string">&quot;zhangyi&quot;</span><span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="punctuation">&#123;</span><span class="attr">&quot;id&quot;</span><span class="punctuation">:</span><span class="string">&quot;2&quot;</span><span class="punctuation">,</span> <span class="attr">&quot;name&quot;</span><span class="punctuation">:</span><span class="string">&quot;haiqing&quot;</span><span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="punctuation">&#123;</span><span class="attr">&quot;id&quot;</span><span class="punctuation">:</span><span class="string">&quot;3&quot;</span><span class="punctuation">,</span> <span class="attr">&quot;name&quot;</span><span class="punctuation">:</span><span class="string">&quot;zhanghanyu&quot;</span><span class="punctuation">&#125;</span></span><br><span class="line">    <span class="punctuation">]</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>

<p>具体 DSL 可自己从网上查询相关资源学习</p>
<h1 id="SpringBoot-整合-Elasticsearch"><a href="#SpringBoot-整合-Elasticsearch" class="headerlink" title="SpringBoot 整合 Elasticsearch"></a>SpringBoot 整合 Elasticsearch</h1><p>SpringBoot 整合 Elasticsearch 有三种方式可供选择，分别是 RestHighLevelClient、ElasticsearchRestTemplate 和 ElasticsearchRepository</p>
<p>这里主要给出 RestHighLevelClient 示例</p>
<ol>
<li>在 ES 中，创建对应的索引</li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">PUT /book_index</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;settings&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;number_of_replicas&quot;</span>: 1,</span><br><span class="line">    <span class="string">&quot;number_of_shards&quot;</span>: 2</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">&quot;mappings&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;properties&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;nameText&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;search_analyzer&quot;</span>: <span class="string">&quot;ik_smart&quot;</span>,</span><br><span class="line">        <span class="string">&quot;analyzer&quot;</span>: <span class="string">&quot;ik_max_word&quot;</span>,</span><br><span class="line">        <span class="string">&quot;type&quot;</span>: <span class="string">&quot;text&quot;</span></span><br><span class="line">      &#125;,</span><br><span class="line">      <span class="string">&quot;author&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;type&quot;</span>:<span class="string">&quot;text&quot;</span>,</span><br><span class="line">        <span class="string">&quot;fields&quot;</span>:&#123;</span><br><span class="line">            <span class="string">&quot;keyword&quot;</span>:&#123;</span><br><span class="line">                <span class="string">&quot;ignore_above&quot;</span>:256,</span><br><span class="line">                <span class="string">&quot;type&quot;</span>:<span class="string">&quot;keyword&quot;</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;,</span><br><span class="line">      <span class="string">&quot;score&quot;</span>:&#123;</span><br><span class="line">          <span class="string">&quot;type&quot;</span>:<span class="string">&quot;float&quot;</span></span><br><span class="line">      &#125;,</span><br><span class="line">      <span class="string">&quot;commentText&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;search_analyzer&quot;</span>: <span class="string">&quot;ik_smart&quot;</span>,</span><br><span class="line">        <span class="string">&quot;analyzer&quot;</span>: <span class="string">&quot;ik_max_word&quot;</span>,</span><br><span class="line">        <span class="string">&quot;type&quot;</span>: <span class="string">&quot;text&quot;</span></span><br><span class="line">      &#125;,</span><br><span class="line">      <span class="string">&quot;publishTime&quot;</span>:&#123;</span><br><span class="line">          <span class="string">&quot;type&quot;</span>:<span class="string">&quot;date&quot;</span>,</span><br><span class="line">          <span class="string">&quot;format&quot;</span>: <span class="string">&quot;yyyy-MM-d HH:mm:ss&quot;</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>创建项目</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.example<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>ElasticsearchDemo<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ol start="3">
<li>导入依赖</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--ES--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.elasticsearch<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>elasticsearch<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>7.9.3<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.elasticsearch.client<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>elasticsearch-rest-high-level-client<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.elasticsearch.client<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>elasticsearch-rest-client<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.elasticsearch.client<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>elasticsearch-rest-high-level-client<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>7.9.3<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.elasticsearch.client<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>elasticsearch-rest-client<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>7.9.3<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!--lombok--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.projectlombok<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>lombok<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.18.22<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!--fastjson--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>fastjson<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.0.3<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ol start="4">
<li>修改 application.properties 配置文件</li>
</ol>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#应用名</span></span><br><span class="line"><span class="attr">spring.application.name</span>=<span class="string">elasticsearch-demo</span></span><br><span class="line"><span class="comment">#服务端口号</span></span><br><span class="line"><span class="attr">server.port</span>=<span class="string">8080</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">#es地址</span></span><br><span class="line"><span class="attr">spring.elasticsearch.uris</span>=<span class="string">http://192.168.8.102:9200</span></span><br></pre></td></tr></table></figure>

<ol start="5">
<li>创建实体类</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.elasticsearch.demo.domain;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> lombok.AllArgsConstructor;</span><br><span class="line"><span class="keyword">import</span> lombok.Data;</span><br><span class="line"><span class="keyword">import</span> lombok.NoArgsConstructor;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> TODO</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="meta">@NoArgsConstructor</span></span><br><span class="line"><span class="meta">@AllArgsConstructor</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Book</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * ID</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String id;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 书名</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String nameText;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 作者</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String author;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 评分</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> Double score;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 评语</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String commentText;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 发布时间</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> Date publishTime;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="6">
<li>创建测试类，进行测试</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.elasticsearch.demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.alibaba.fastjson2.JSON;</span><br><span class="line"><span class="keyword">import</span> com.example.elasticsearch.demo.domain.Book;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.action.DocWriteRequest;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.action.bulk.BulkRequest;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.action.bulk.BulkResponse;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.action.delete.DeleteRequest;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.action.delete.DeleteResponse;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.action.get.GetRequest;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.action.get.GetResponse;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.action.index.IndexRequest;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.action.index.IndexResponse;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.action.search.SearchRequest;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.action.search.SearchResponse;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.action.support.master.AcknowledgedResponse;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.client.RequestOptions;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.client.RestHighLevelClient;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.client.indices.CreateIndexRequest;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.client.indices.CreateIndexResponse;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.client.indices.GetIndexRequest;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.client.indices.GetIndexResponse;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.common.xcontent.XContentType;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.index.query.*;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.index.reindex.BulkByScrollResponse;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.index.reindex.DeleteByQueryRequest;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.search.SearchHit;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.search.builder.SearchSourceBuilder;</span><br><span class="line"><span class="keyword">import</span> org.elasticsearch.search.sort.SortOrder;</span><br><span class="line"><span class="keyword">import</span> org.junit.jupiter.api.Test;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.test.context.SpringBootTest;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.text.SimpleDateFormat;</span><br><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="meta">@SpringBootTest</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">ElasticsearchDemoApplicationTests</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> RestHighLevelClient restHighLevelClient;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="type">SimpleDateFormat</span> <span class="variable">simpleDateFormat</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SimpleDateFormat</span>(<span class="string">&quot;yyyy-MM-dd HH:mm:ss&quot;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="type">String</span> <span class="variable">index</span> <span class="operator">=</span> <span class="string">&quot;book_index&quot;</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">final</span> <span class="type">String</span> <span class="variable">routing</span> <span class="operator">=</span> <span class="string">&quot;test&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// todo === === === === === === === === index === === === === === === === ===</span></span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">indexIsExists</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="type">GetIndexRequest</span> <span class="variable">indexRequest</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">GetIndexRequest</span>(index);</span><br><span class="line">        <span class="type">boolean</span> <span class="variable">exists</span> <span class="operator">=</span> restHighLevelClient.indices().exists(indexRequest, RequestOptions.DEFAULT);</span><br><span class="line">        System.out.println(<span class="string">&quot;指定 index 是否存在: &quot;</span> + exists);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 创建索引</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">createIndex</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="type">CreateIndexRequest</span> <span class="variable">indexRequest</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">CreateIndexRequest</span>(index);</span><br><span class="line">        <span class="type">CreateIndexResponse</span> <span class="variable">indexResponse</span> <span class="operator">=</span> restHighLevelClient.indices().create(indexRequest, RequestOptions.DEFAULT);</span><br><span class="line">        System.out.println(<span class="string">&quot;创建 index：&quot;</span> + JSON.toJSONString(indexResponse));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 删除索引</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">deleteIndex</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="type">DeleteIndexRequest</span> <span class="variable">indexRequest</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">DeleteIndexRequest</span>(index);</span><br><span class="line">        <span class="type">AcknowledgedResponse</span> <span class="variable">delete</span> <span class="operator">=</span> restHighLevelClient.indices().delete(indexRequest, RequestOptions.DEFAULT);</span><br><span class="line">        System.out.println(<span class="string">&quot;删除 index：&quot;</span> + JSON.toJSONString(delete));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 查询索引信息</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">queryIndex</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="type">GetIndexRequest</span> <span class="variable">indexRequest</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">GetIndexRequest</span>(index);</span><br><span class="line">        <span class="type">GetIndexResponse</span> <span class="variable">indexResponse</span> <span class="operator">=</span> restHighLevelClient.indices().get(indexRequest, RequestOptions.DEFAULT);</span><br><span class="line">        <span class="comment">//index 索引信息：&#123;&quot;book_index&quot;:&#123;&quot;sourceAsMap&quot;:&#123;&quot;properties&quot;:&#123;&quot;publishTime&quot;:&#123;&quot;format&quot;:&quot;yyyy-MM-d HH:mm:ss&quot;,&quot;type&quot;:&quot;date&quot;&#125;,&quot;score&quot;:&#123;&quot;type&quot;:&quot;float&quot;&#125;,&quot;author&quot;:&#123;&quot;type&quot;:&quot;text&quot;,&quot;fields&quot;:&#123;&quot;keyword&quot;:&#123;&quot;ignore_above&quot;:256,&quot;type&quot;:&quot;keyword&quot;&#125;&#125;&#125;,&quot;nameText&quot;:&#123;&quot;search_analyzer&quot;:&quot;ik_smart&quot;,&quot;analyzer&quot;:&quot;ik_max_word&quot;,&quot;type&quot;:&quot;text&quot;&#125;,&quot;commentText&quot;:&#123;&quot;search_analyzer&quot;:&quot;ik_smart&quot;,&quot;analyzer&quot;:&quot;ik_max_word&quot;,&quot;type&quot;:&quot;text&quot;&#125;&#125;&#125;&#125;&#125;</span></span><br><span class="line">        System.out.println(<span class="string">&quot;index 索引信息：&quot;</span> + JSON.toJSONString(indexResponse.getMappings()));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// todo === === === === === === === === document === === === === === === === ===</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 插入文档</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">insertDocument</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="type">Book</span> <span class="variable">book</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Book</span>(<span class="string">&quot;1001&quot;</span>, <span class="string">&quot;西游记&quot;</span>, <span class="string">&quot;吴承恩&quot;</span>, <span class="number">8.7</span>,</span><br><span class="line">                <span class="string">&quot;《西游记》的作者是明代的吴承恩。这是一本古代神话小说。它主要写了唐僧师徒四人，经历千辛万苦经过九九八十一难，到西天取回真经的故事&quot;</span>, simpleDateFormat.format(<span class="keyword">new</span> <span class="title class_">Date</span>()));</span><br><span class="line">        <span class="comment">//构建IndexRequest</span></span><br><span class="line">        <span class="type">IndexRequest</span> <span class="variable">indexRequest</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">IndexRequest</span>()</span><br><span class="line">                .index(index) <span class="comment">//索引</span></span><br><span class="line">                .routing(routing) <span class="comment">//路由/租户</span></span><br><span class="line">                .id(book.getId()).opType(DocWriteRequest.OpType.CREATE) <span class="comment">//id，不指定id，可自动生成</span></span><br><span class="line">                .source(JSON.toJSONString(book), XContentType.JSON);</span><br><span class="line">        <span class="comment">//插入获取结果</span></span><br><span class="line">        <span class="type">IndexResponse</span> <span class="variable">indexResponse</span> <span class="operator">=</span> restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);</span><br><span class="line">        <span class="comment">//插入结果：&#123;&quot;fragment&quot;:false,&quot;id&quot;:&quot;1001&quot;,&quot;index&quot;:&quot;book_index&quot;,&quot;primaryTerm&quot;:1,&quot;result&quot;:0,&quot;seqNo&quot;:0,&quot;shardId&quot;:&#123;&quot;fragment&quot;:true,&quot;id&quot;:-1,&quot;index&quot;:&#123;&quot;UUID&quot;:&quot;_na_&quot;,&quot;fragment&quot;:false,&quot;name&quot;:&quot;book_index&quot;&#125;,&quot;indexName&quot;:&quot;book_index&quot;&#125;,&quot;shardInfo&quot;:&#123;&quot;failed&quot;:0,&quot;failures&quot;:[],&quot;fragment&quot;:false,&quot;successful&quot;:2,&quot;total&quot;:2&#125;,&quot;type&quot;:&quot;_doc&quot;,&quot;version&quot;:1&#125;</span></span><br><span class="line">        System.out.println(<span class="string">&quot;插入结果：&quot;</span> + JSON.toJSONString(indexResponse));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 批量插入文档</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">insertBatchDocument</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        List&lt;Book&gt; books = Arrays.asList(</span><br><span class="line"><span class="comment">//                new Book(&quot;1002&quot;, &quot;红楼梦&quot;, &quot;曹雪芹&quot;, 9.9,</span></span><br><span class="line"><span class="comment">//                        &quot;细读《红楼梦》，感慨于他们的悲欢离合，更是在品味人生的匆匆。旧时王谢堂前燕，早已飞入寻常百姓之家。纵使繁华如贾府，也逃不了时光的推动。&quot; +</span></span><br><span class="line"><span class="comment">//                                &quot;盛极则衰，时光，多么盛大的舞台，昨日可重现脑海，今宵，却已别有一番洞天&quot;, simpleDateFormat.format(new Date())),</span></span><br><span class="line"><span class="comment">//                new Book(&quot;1003&quot;, &quot;水浒传&quot;, &quot;施耐庵&quot;, 9.9,</span></span><br><span class="line"><span class="comment">//                        &quot;每个人都有梦想，但如果不行动，梦想只会成为泡影。所以，从此时起，我要用辛勤的努力换来梦想的实现&quot;, simpleDateFormat.format(new Date())),</span></span><br><span class="line"><span class="comment">//                new Book(&quot;1004&quot;, &quot;三国演义&quot;, &quot;罗贯中&quot;, 9.9, &quot;三国的每个人都是我们的老师，我们要从他们的身上学到各种优良的品质&quot;, simpleDateFormat.format(new Date())),</span></span><br><span class="line"><span class="comment">//                new Book(&quot;1001&quot;, &quot;西游记&quot;, &quot;吴承恩&quot;, 8.7,</span></span><br><span class="line"><span class="comment">//                        &quot;《西游记》的作者是明代的吴承恩。这是一本古代神话小说。它主要写了唐僧师徒四人，经历千辛万苦经过九九八十一难，到西天取回真经的故事&quot;, simpleDateFormat.format(new Date())),</span></span><br><span class="line"><span class="comment">//                new Book(&quot;1005&quot;, &quot;三体&quot;, &quot;刘慈欣&quot;, 9.9,</span></span><br><span class="line"><span class="comment">//                        &quot;《三体》这本书讲述三体文明与地球文明之间的碰撞，三体作为外太空另一个可适宜生物居住的星球同样也居住着三体人，三体人把地球人当作敌人，&quot; +</span></span><br><span class="line"><span class="comment">//                                &quot;地球人却对他们的看法褒贬不一于是地球上展开了一场人类关于三体的激烈斗争&quot;, simpleDateFormat.format(new Date())),</span></span><br><span class="line">                <span class="keyword">new</span> <span class="title class_">Book</span>(<span class="string">&quot;1006&quot;</span>, <span class="string">&quot;东游记&quot;</span>, <span class="string">&quot;张三&quot;</span>, <span class="number">3.9</span>, <span class="string">&quot;小说三体人寻常百姓老师&quot;</span>, simpleDateFormat.format(<span class="keyword">new</span> <span class="title class_">Date</span>()))</span><br><span class="line">        );</span><br><span class="line">        <span class="comment">//构建BulkRequest</span></span><br><span class="line">        <span class="type">BulkRequest</span> <span class="variable">bulkRequest</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BulkRequest</span>();</span><br><span class="line">        <span class="keyword">for</span> (Book book : books) &#123;</span><br><span class="line">            <span class="type">IndexRequest</span> <span class="variable">indexRequest</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">IndexRequest</span>()</span><br><span class="line">                    .index(index)</span><br><span class="line">                    .routing(routing)</span><br><span class="line">                    .id(book.getId())</span><br><span class="line">                    .source(JSON.toJSONString(book), XContentType.JSON);</span><br><span class="line">            bulkRequest.add(indexRequest);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//插入获取结果</span></span><br><span class="line">        <span class="type">BulkResponse</span> <span class="variable">bulkItemResponses</span> <span class="operator">=</span> restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);</span><br><span class="line">        <span class="comment">//插入结果：[&#123;&quot;failed&quot;:false,&quot;fragment&quot;:false,&quot;id&quot;:&quot;1002&quot;,&quot;index&quot;:&quot;book_index&quot;,&quot;itemId&quot;:0,&quot;opType&quot;:0,&quot;response&quot;:&#123;&quot;fragment&quot;:false,&quot;id&quot;:&quot;1002&quot;,&quot;index&quot;:&quot;book_index&quot;,&quot;primaryTerm&quot;:1,&quot;result&quot;:0,&quot;seqNo&quot;:1,&quot;shardId&quot;:&#123;&quot;fragment&quot;:true,&quot;id&quot;:-1,&quot;index&quot;:&#123;&quot;UUID&quot;:&quot;_na_&quot;,&quot;fragment&quot;:false,&quot;name&quot;:&quot;book_index&quot;&#125;,&quot;indexName&quot;:&quot;book_index&quot;&#125;,&quot;shardInfo&quot;:&#123;&quot;failed&quot;:0,&quot;failures&quot;:[],&quot;fragment&quot;:false,&quot;successful&quot;:2,&quot;total&quot;:2&#125;,&quot;type&quot;:&quot;_doc&quot;,&quot;version&quot;:1&#125;,&quot;type&quot;:&quot;_doc&quot;,&quot;version&quot;:1&#125;,&#123;&quot;failed&quot;:false,&quot;fragment&quot;:false,&quot;id&quot;:&quot;1003&quot;,&quot;index&quot;:&quot;book_index&quot;,&quot;itemId&quot;:1,&quot;opType&quot;:0,&quot;response&quot;:&#123;&quot;fragment&quot;:false,&quot;id&quot;:&quot;1003&quot;,&quot;index&quot;:&quot;book_index&quot;,&quot;primaryTerm&quot;:1,&quot;result&quot;:0,&quot;seqNo&quot;:2,&quot;shardId&quot;:&#123;&quot;fragment&quot;:true,&quot;id&quot;:-1,&quot;index&quot;:&#123;&quot;UUID&quot;:&quot;_na_&quot;,&quot;fragment&quot;:false,&quot;name&quot;:&quot;book_index&quot;&#125;,&quot;indexName&quot;:&quot;book_index&quot;&#125;,&quot;shardInfo&quot;:&#123;&quot;failed&quot;:0,&quot;failures&quot;:[],&quot;fragment&quot;:false,&quot;successful&quot;:2,&quot;total&quot;:2&#125;,&quot;type&quot;:&quot;_doc&quot;,&quot;version&quot;:1&#125;,&quot;type&quot;:&quot;_doc&quot;,&quot;version&quot;:1&#125;,&#123;&quot;failed&quot;:false,&quot;fragment&quot;:false,&quot;id&quot;:&quot;1004&quot;,&quot;index&quot;:&quot;book_index&quot;,&quot;itemId&quot;:2,&quot;opType&quot;:0,&quot;response&quot;:&#123;&quot;fragment&quot;:false,&quot;id&quot;:&quot;1004&quot;,&quot;index&quot;:&quot;book_index&quot;,&quot;primaryTerm&quot;:1,&quot;result&quot;:0,&quot;seqNo&quot;:3,&quot;shardId&quot;:&#123;&quot;fragment&quot;:true,&quot;id&quot;:-1,&quot;index&quot;:&#123;&quot;UUID&quot;:&quot;_na_&quot;,&quot;fragment&quot;:false,&quot;name&quot;:&quot;book_index&quot;&#125;,&quot;indexName&quot;:&quot;book_index&quot;&#125;,&quot;shardInfo&quot;:&#123;&quot;failed&quot;:0,&quot;failures&quot;:[],&quot;fragment&quot;:false,&quot;successful&quot;:2,&quot;total&quot;:2&#125;,&quot;type&quot;:&quot;_doc&quot;,&quot;version&quot;:1&#125;,&quot;type&quot;:&quot;_doc&quot;,&quot;version&quot;:1&#125;]</span></span><br><span class="line">        System.out.println(<span class="string">&quot;插入结果：&quot;</span> + JSON.toJSONString(bulkItemResponses));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 根据id删除文档</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">deleteDocument</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="type">DeleteRequest</span> <span class="variable">deleteRequest</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">DeleteRequest</span>().index(index).routing(routing).id(<span class="string">&quot;1001&quot;</span>);</span><br><span class="line">        <span class="type">DeleteResponse</span> <span class="variable">deleteResponse</span> <span class="operator">=</span> restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);</span><br><span class="line">        <span class="comment">//删除结果：&#123;&quot;fragment&quot;:false,&quot;id&quot;:&quot;1001&quot;,&quot;index&quot;:&quot;book_index&quot;,&quot;primaryTerm&quot;:1,&quot;result&quot;:2,&quot;seqNo&quot;:4,&quot;shardId&quot;:&#123;&quot;fragment&quot;:true,&quot;id&quot;:-1,&quot;index&quot;:&#123;&quot;UUID&quot;:&quot;_na_&quot;,&quot;fragment&quot;:false,&quot;name&quot;:&quot;book_index&quot;&#125;,&quot;indexName&quot;:&quot;book_index&quot;&#125;,&quot;shardInfo&quot;:&#123;&quot;failed&quot;:0,&quot;failures&quot;:[],&quot;fragment&quot;:false,&quot;successful&quot;:2,&quot;total&quot;:2&#125;,&quot;type&quot;:&quot;_doc&quot;,&quot;version&quot;:2&#125;</span></span><br><span class="line">        System.out.println(<span class="string">&quot;删除结果：&quot;</span> + JSON.toJSONString(deleteResponse));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 条件删除</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">queryDeletDocument</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="type">DeleteByQueryRequest</span> <span class="variable">deleteByQueryRequest</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">DeleteByQueryRequest</span>(index)</span><br><span class="line">                .setRouting(routing)</span><br><span class="line">                .setQuery(QueryBuilders.boolQuery().must(QueryBuilders.matchQuery(<span class="string">&quot;author&quot;</span>, <span class="string">&quot;罗贯中&quot;</span>)));</span><br><span class="line">        <span class="type">BulkByScrollResponse</span> <span class="variable">deleteByQuery</span> <span class="operator">=</span> restHighLevelClient.deleteByQuery(deleteByQueryRequest, RequestOptions.DEFAULT);</span><br><span class="line">        <span class="comment">//删除结果：&#123;&quot;batches&quot;:1,&quot;bulkFailures&quot;:[],&quot;bulkRetries&quot;:0,&quot;created&quot;:0,&quot;deleted&quot;:1,&quot;fragment&quot;:true,&quot;noops&quot;:0,&quot;searchFailures&quot;:[],&quot;searchRetries&quot;:0,&quot;status&quot;:&#123;&quot;batches&quot;:1,&quot;bulkRetries&quot;:0,&quot;created&quot;:0,&quot;deleted&quot;:1,&quot;fragment&quot;:false,&quot;noops&quot;:0,&quot;requestsPerSecond&quot;:Infinity,&quot;searchRetries&quot;:0,&quot;sliceStatuses&quot;:[],&quot;successfullyProcessed&quot;:1,&quot;throttled&quot;:&#123;&quot;days&quot;:0,&quot;daysFrac&quot;:0.0,&quot;hours&quot;:0,&quot;hoursFrac&quot;:0.0,&quot;micros&quot;:0,&quot;microsFrac&quot;:0.0,&quot;millis&quot;:0,&quot;millisFrac&quot;:0.0,&quot;minutes&quot;:0,&quot;minutesFrac&quot;:0.0,&quot;nanos&quot;:0,&quot;seconds&quot;:0,&quot;secondsFrac&quot;:0.0,&quot;stringRep&quot;:&quot;0ms&quot;&#125;,&quot;throttledUntil&quot;:&#123;&quot;days&quot;:0,&quot;daysFrac&quot;:0.0,&quot;hours&quot;:0,&quot;hoursFrac&quot;:0.0,&quot;micros&quot;:0,&quot;microsFrac&quot;:0.0,&quot;millis&quot;:0,&quot;millisFrac&quot;:0.0,&quot;minutes&quot;:0,&quot;minutesFrac&quot;:0.0,&quot;nanos&quot;:0,&quot;seconds&quot;:0,&quot;secondsFrac&quot;:0.0,&quot;stringRep&quot;:&quot;0ms&quot;&#125;,&quot;total&quot;:1,&quot;updated&quot;:0,&quot;versionConflicts&quot;:0,&quot;writeableName&quot;:&quot;bulk-by-scroll&quot;&#125;,&quot;timedOut&quot;:false,&quot;took&quot;:&#123;&quot;days&quot;:0,&quot;daysFrac&quot;:6.712962962962963E-6,&quot;hours&quot;:0,&quot;hoursFrac&quot;:1.611111111111111E-4,&quot;micros&quot;:580000,&quot;microsFrac&quot;:580000.0,&quot;millis&quot;:580,&quot;millisFrac&quot;:580.0,&quot;minutes&quot;:0,&quot;minutesFrac&quot;:0.009666666666666667,&quot;nanos&quot;:580000000,&quot;seconds&quot;:0,&quot;secondsFrac&quot;:0.58,&quot;stringRep&quot;:&quot;580ms&quot;&#125;,&quot;total&quot;:1,&quot;updated&quot;:0,&quot;versionConflicts&quot;:0&#125;</span></span><br><span class="line">        System.out.println(<span class="string">&quot;删除结果：&quot;</span> + JSON.toJSONString(deleteByQuery));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 根据id修改文档</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">updateDocument</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        实现逻辑一（参数：id和修改字段）：</span></span><br><span class="line"><span class="comment">          1、根据id查询出指定的文档</span></span><br><span class="line"><span class="comment">          2、获取结果，并转换为 ContentDTO 对象，然后修改指定字段</span></span><br><span class="line"><span class="comment">          3、修改完成后，重新插入</span></span><br><span class="line"><span class="comment">        实现逻辑二（参数：id和已修改的对象）：</span></span><br><span class="line"><span class="comment">          1、直接删除根据id删除原有数据</span></span><br><span class="line"><span class="comment">          2、把修改后的对象，直接插入（插入时，需要指定id，否则id就会随机指定）</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 根据id查询文档</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">queryDocument</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="type">GetRequest</span> <span class="variable">getRequest</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">GetRequest</span>().index(index).routing(routing).id(<span class="string">&quot;1003&quot;</span>);</span><br><span class="line">        <span class="type">GetResponse</span> <span class="variable">getResponse</span> <span class="operator">=</span> restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);</span><br><span class="line">        <span class="comment">//查询文档：&#123;&quot;author&quot;:&quot;施耐庵&quot;,&quot;commentText&quot;:&quot;每个人都有梦想，但如果不行动，梦想只会成为泡影。所以，从此时起，我要用辛勤的努力换来梦想的实现&quot;,&quot;id&quot;:&quot;1003&quot;,&quot;nameText&quot;:&quot;水浒传&quot;,&quot;publishTime&quot;:&quot;2023-09-16 07:42:18&quot;,&quot;score&quot;:9.9&#125;</span></span><br><span class="line">        System.out.println(<span class="string">&quot;查询文档：&quot;</span> + getResponse.getSourceAsString());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * match查询</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">matchQueryDocument</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">// 查询所有</span></span><br><span class="line"><span class="comment">//        MatchAllQueryBuilder queryBuilder = QueryBuilders.matchAllQuery();</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 匹配条件查询</span></span><br><span class="line"><span class="comment">//        MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery(&quot;author&quot;, &quot;罗贯中&quot;);</span></span><br><span class="line">        <span class="comment">//commentText使用分词,查询时命中分词后结果即可被查询出来</span></span><br><span class="line"><span class="comment">//        MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery(&quot;commentText&quot;, &quot;小说&quot;);</span></span><br><span class="line">        <span class="type">MatchQueryBuilder</span> <span class="variable">queryBuilder</span> <span class="operator">=</span> QueryBuilders.matchQuery(<span class="string">&quot;score&quot;</span>, <span class="number">9.9</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 短语查询</span></span><br><span class="line"><span class="comment">//        MatchPhraseQueryBuilder queryBuilder = QueryBuilders.matchPhraseQuery(&quot;commentText&quot;, &quot;古代神话小说&quot;);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 通配符查询</span></span><br><span class="line"><span class="comment">//        WildcardQueryBuilder queryBuilder = QueryBuilders.wildcardQuery(&quot;nameText&quot;, &quot;*记*&quot;);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 属性级联，通过.来实现</span></span><br><span class="line"><span class="comment">//        WildcardQueryBuilder queryBuilder = QueryBuilders.wildcardQuery(&quot;actorList.name&quot;, &quot;张*&quot;);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 查询构建器</span></span><br><span class="line">        <span class="type">SearchSourceBuilder</span> <span class="variable">searchSourceBuilder</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SearchSourceBuilder</span>();</span><br><span class="line">        searchSourceBuilder.query(queryBuilder);</span><br><span class="line">        <span class="comment">// 查询请求</span></span><br><span class="line">        <span class="type">SearchRequest</span> <span class="variable">searchRequest</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SearchRequest</span>(index).routing(routing).source(searchSourceBuilder);</span><br><span class="line">        System.out.println(searchRequest);</span><br><span class="line">        <span class="comment">// 执行查询</span></span><br><span class="line">        <span class="type">SearchResponse</span> <span class="variable">searchResponse</span> <span class="operator">=</span> restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);</span><br><span class="line">        System.out.println(<span class="string">&quot;查询结果:&quot;</span>);</span><br><span class="line">        <span class="keyword">for</span> (SearchHit hit : searchResponse.getHits().getHits()) &#123;</span><br><span class="line">            System.out.println(hit.getSourceAsString());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 条件查询</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">boolQueryDocument</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="type">BoolQueryBuilder</span> <span class="variable">queryBuilder</span> <span class="operator">=</span> QueryBuilders.boolQuery();</span><br><span class="line">        <span class="comment">// score = 9.9</span></span><br><span class="line">        queryBuilder.must(QueryBuilders.matchQuery(<span class="string">&quot;score&quot;</span>, <span class="number">9.9</span>));</span><br><span class="line">        <span class="comment">// label != Redis</span></span><br><span class="line">        queryBuilder.mustNot(QueryBuilders.wildcardQuery(<span class="string">&quot;id&quot;</span>, <span class="string">&quot;1003&quot;</span>));</span><br><span class="line"></span><br><span class="line">        <span class="type">SearchSourceBuilder</span> <span class="variable">searchSourceBuilder</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SearchSourceBuilder</span>().query(queryBuilder);</span><br><span class="line">        <span class="type">SearchRequest</span> <span class="variable">searchRequest</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SearchRequest</span>().indices(index).routing(routing).source(searchSourceBuilder);</span><br><span class="line">        <span class="type">SearchResponse</span> <span class="variable">searchResponse</span> <span class="operator">=</span> restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);</span><br><span class="line">        System.out.println(<span class="string">&quot;查询结果:&quot;</span>);</span><br><span class="line">        <span class="keyword">for</span> (SearchHit hit : searchResponse.getHits().getHits()) &#123;</span><br><span class="line">            System.out.println(hit.getSourceAsString());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 分页查询</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">pageQueryDocument</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="type">MatchAllQueryBuilder</span> <span class="variable">queryBuilder</span> <span class="operator">=</span> QueryBuilders.matchAllQuery();</span><br><span class="line"></span><br><span class="line">        <span class="type">SearchSourceBuilder</span> <span class="variable">searchSourceBuilder</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SearchSourceBuilder</span>();</span><br><span class="line">        searchSourceBuilder.from(<span class="number">1</span>);</span><br><span class="line">        searchSourceBuilder.size(<span class="number">2</span>);</span><br><span class="line">        searchSourceBuilder.sort(<span class="string">&quot;score&quot;</span>, SortOrder.ASC);</span><br><span class="line">        searchSourceBuilder.query(queryBuilder);</span><br><span class="line"></span><br><span class="line">        <span class="type">SearchRequest</span> <span class="variable">searchRequest</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SearchRequest</span>().indices(index).routing(routing).source(searchSourceBuilder);</span><br><span class="line">        <span class="type">SearchResponse</span> <span class="variable">searchResponse</span> <span class="operator">=</span> restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);</span><br><span class="line">        System.out.println(<span class="string">&quot;查询结果:&quot;</span>);</span><br><span class="line">        <span class="keyword">for</span> (SearchHit hit : searchResponse.getHits().getHits()) &#123;</span><br><span class="line">            System.out.println(hit.getSourceAsString());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>











































</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Oct25-X</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://oct25-x.atomgit.net/oct25-xxxxx/2023/10/24/33%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E4%BC%81%E4%B8%9A%E7%BA%A7%E6%90%9C%E7%B4%A2%E5%BC%95%E6%93%8E%20ElashticSearch_7.9.3/">https://oct25-x.atomgit.net/oct25-xxxxx/2023/10/24/33%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E4%BC%81%E4%B8%9A%E7%BA%A7%E6%90%9C%E7%B4%A2%E5%BC%95%E6%93%8E%20ElashticSearch_7.9.3/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://oct25-x.atomgit.net/oct25-xxxxx" target="_blank">Oct25-X</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/oct25-xxxxx/tags/Elasticsearch/">Elasticsearch</a></div><div class="post_share"><div class="social-share" data-image="/oct25-xxxxx/img/cover_img/ctt33.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/oct25-xxxxx/2023/10/27/34%E5%BC%82%E6%9E%84%E6%95%B0%E6%8D%AE%E6%BA%90%E5%90%8C%E6%AD%A5%E5%B7%A5%E5%85%B7DataX/"><img class="prev-cover" src="/oct25-xxxxx/img/cover_img/ctt34.png" onerror="onerror=null;src='/oct25-xxxxx/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">异构数据源同步工具DataX</div></div></a></div><div class="next-post pull-right"><a href="/oct25-xxxxx/2023/10/20/32Spring%E5%AE%89%E5%85%A8%E6%A1%86%E6%9E%B6SpringSecurity/"><img class="next-cover" src="/oct25-xxxxx/img/cover_img/ctt32.png" onerror="onerror=null;src='/oct25-xxxxx/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Spring 安全框架 SpringSecurity</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/oct25-xxxxx/img/avatar.jpg" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Oct25-X</div><div class="author-info__description"></div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/oct25-xxxxx/archives/"><div class="headline">文章</div><div class="length-num">52</div></a></div><div class="card-info-data-item"><a href="/oct25-xxxxx/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div><div class="card-info-data-item"><a href="/oct25-xxxxx/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Elasticsearch-%E7%AE%80%E4%BB%8B"><span class="toc-number">1.</span> <span class="toc-text">Elasticsearch 简介</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81ElashticSearch-%E4%BD%BF%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="toc-number">1.1.</span> <span class="toc-text">一、ElashticSearch 使用场景</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E4%B8%8E%E5%85%B6%E4%BB%96%E6%95%B0%E6%8D%AE%E5%AD%98%E5%82%A8%E8%BF%9B%E8%A1%8C%E6%AF%94%E8%BE%83"><span class="toc-number">1.2.</span> <span class="toc-text">二、与其他数据存储进行比较</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81ElashticSearch-%E7%9A%84%E7%89%B9%E7%82%B9"><span class="toc-number">1.3.</span> <span class="toc-text">三、ElashticSearch 的特点</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%A9%E7%84%B6%E5%88%86%E7%89%87%EF%BC%8C%E5%A4%A9%E7%84%B6%E9%9B%86%E7%BE%A4"><span class="toc-number">1.3.1.</span> <span class="toc-text">天然分片，天然集群</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%A9%E7%84%B6%E7%B4%A2%E5%BC%95"><span class="toc-number">1.3.2.</span> <span class="toc-text">天然索引</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BC%A0%E7%BB%9F%E5%85%B3%E7%B3%BB%E5%9E%8B%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="toc-number">1.3.2.1.</span> <span class="toc-text">传统关系型数据库</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%80%92%E6%8E%92%E7%B4%A2%E5%BC%95%E6%98%AF%E5%A6%82%E4%BD%95%E5%A4%84%E7%90%86%E7%9A%84"><span class="toc-number">1.3.2.2.</span> <span class="toc-text">倒排索引是如何处理的</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%B4%A2%E5%BC%95%E7%BB%93%E6%9E%84%E5%AF%B9%E6%AF%94"><span class="toc-number">1.3.2.3.</span> <span class="toc-text">索引结构对比</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Elasticsearch-%E5%AE%89%E8%A3%85"><span class="toc-number">2.</span> <span class="toc-text">Elasticsearch 安装</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E7%9B%B8%E5%85%B3%E4%B8%8B%E8%BD%BD"><span class="toc-number">2.1.</span> <span class="toc-text">一、相关下载</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81Windows-%E5%AE%89%E8%A3%85-Elasticsearch"><span class="toc-number">2.2.</span> <span class="toc-text">二、Windows 安装 Elasticsearch</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81Linux-%E5%AE%89%E8%A3%85-Elasticsearch"><span class="toc-number">2.3.</span> <span class="toc-text">三、Linux 安装 Elasticsearch</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9-Linux-%E9%85%8D%E7%BD%AE"><span class="toc-number">2.3.1.</span> <span class="toc-text">修改 Linux 配置</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%97%AE%E9%A2%981"><span class="toc-number">2.3.1.1.</span> <span class="toc-text">问题1</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%97%AE%E9%A2%982"><span class="toc-number">2.3.1.2.</span> <span class="toc-text">问题2</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%97%AE%E9%A2%983"><span class="toc-number">2.3.1.3.</span> <span class="toc-text">问题3</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Elasticsearch-%E5%AE%89%E8%A3%85-1"><span class="toc-number">2.3.2.</span> <span class="toc-text">Elasticsearch 安装</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8%E4%B8%8E%E5%85%B3%E9%97%AD"><span class="toc-number">2.3.3.</span> <span class="toc-text">启动与关闭</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Elasticsearch-%E9%9B%86%E7%BE%A4"><span class="toc-number">2.3.4.</span> <span class="toc-text">Elasticsearch 集群</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95-ES"><span class="toc-number">2.3.5.</span> <span class="toc-text">测试 ES</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E3%80%81Kibana-%E5%AE%89%E8%A3%85"><span class="toc-number">2.4.</span> <span class="toc-text">四、Kibana 安装</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94%E3%80%81Elasticsearch-%E4%B8%AD%E6%96%87%E5%88%86%E8%AF%8D%E5%99%A8%E5%AE%89%E8%A3%85"><span class="toc-number">2.5.</span> <span class="toc-text">五、Elasticsearch 中文分词器安装</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#ElashticSearch-%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="toc-number">3.</span> <span class="toc-text">ElashticSearch 的基本概念</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Elasticsearch-Restful-Api-DSL"><span class="toc-number">4.</span> <span class="toc-text">Elasticsearch Restful Api (DSL)</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#SpringBoot-%E6%95%B4%E5%90%88-Elasticsearch"><span class="toc-number">5.</span> <span class="toc-text">SpringBoot 整合 Elasticsearch</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/24/52%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala09_%E9%9A%90%E5%BC%8F%E8%BD%AC%E6%8D%A2%E3%80%81%E5%BC%82%E5%B8%B8%E5%92%8C%E6%B3%9B%E5%9E%8B/" title="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型"><img src="/oct25-xxxxx/img/cover_img/ctt52.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/24/52%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala09_%E9%9A%90%E5%BC%8F%E8%BD%AC%E6%8D%A2%E3%80%81%E5%BC%82%E5%B8%B8%E5%92%8C%E6%B3%9B%E5%9E%8B/" title="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型">大数据之多范式编程语言 Scala09_隐式转换、异常和泛型</a><time datetime="2024-02-24T04:55:34.000Z" title="发表于 2024-02-24 12:55:34">2024-02-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/24/51%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala08_%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D/" title="大数据之多范式编程语言 Scala08_模式匹配"><img src="/oct25-xxxxx/img/cover_img/ctt51.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala08_模式匹配"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/24/51%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala08_%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D/" title="大数据之多范式编程语言 Scala08_模式匹配">大数据之多范式编程语言 Scala08_模式匹配</a><time datetime="2024-02-24T00:46:12.000Z" title="发表于 2024-02-24 08:46:12">2024-02-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/" title="大数据之多范式编程语言 Scala07_集合"><img src="/oct25-xxxxx/img/cover_img/ctt50.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala07_集合"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/" title="大数据之多范式编程语言 Scala07_集合">大数据之多范式编程语言 Scala07_集合</a><time datetime="2024-02-22T08:16:10.000Z" title="发表于 2024-02-22 16:16:10">2024-02-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="大数据之多范式编程语言 Scala06_面向对象"><img src="/oct25-xxxxx/img/cover_img/ctt49.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala06_面向对象"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="大数据之多范式编程语言 Scala06_面向对象">大数据之多范式编程语言 Scala06_面向对象</a><time datetime="2024-02-21T07:01:10.000Z" title="发表于 2024-02-21 15:01:10">2024-02-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="大数据之多范式编程语言 Scala05_函数式编程"><img src="/oct25-xxxxx/img/cover_img/ctt48.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala05_函数式编程"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="大数据之多范式编程语言 Scala05_函数式编程">大数据之多范式编程语言 Scala05_函数式编程</a><time datetime="2024-02-20T12:05:11.000Z" title="发表于 2024-02-20 20:05:11">2024-02-20</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/oct25-xxxxx/img/top_img/top_bg7.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2024 By Oct25-X</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">本地搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div id="rightMenu"><div class="rightMenu-group rightMenu-small"><div class="rightMenu-item" id="menu-backward"><i class="fa-solid fa-arrow-left"></i></div><div class="rightMenu-item" id="menu-forward"><i class="fa-solid fa-arrow-right"></i></div><div class="rightMenu-item" id="menu-refresh"><i class="fa-solid fa-arrow-rotate-right"></i></div><div class="rightMenu-item" id="menu-home"><i class="fa-solid fa-house"></i></div></div><div class="rightMenu-group rightMenu-line rightMenuOther"><a class="rightMenu-item menu-link" href="/archives/"><i class="fa-solid fa-archive"></i><span>文章归档</span></a><a class="rightMenu-item menu-link" href="/categories/"><i class="fa-solid fa-folder-open"></i><span>文章分类</span></a><a class="rightMenu-item menu-link" href="/tags/"><i class="fa-solid fa-tags"></i><span>文章标签</span></a></div><div class="rightMenu-group rightMenu-line rightMenuNormal"><a class="rightMenu-item" id="menu-translate"><i class="fa-solid fa-earth-asia"></i><span>繁简切换</span></a><div class="rightMenu-item" id="menu-darkmode"><i class="fa-solid fa-moon"></i><span>切换模式</span></div></div></div><div id="rightmenu-mask"></div><div><script src="/oct25-xxxxx/js/utils.js"></script><script src="/oct25-xxxxx/js/main.js"></script><script src="/oct25-xxxxx/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="/oct25-xxxxx/js/search/local-search.js"></script><div class="js-pjax"></div><script defer src="https://npm.elemecdn.com/jquery@latest/dist/jquery.min.js"></script><script defer data-pjax src="/js/rightMenu.js"></script><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>